/*
 * @Description:
 * @version: 1.0.0
 * @Author: YinJianFeng
 * @Date: 2024-03-29 10:15:27
 */
/**
 * @author YinJianFeng
 * @since 2024-01-10
 */

import { ContextMenu, IContextMenu, MenuItem } from '@amcax/base';
import { sketchEventCenter } from '../../events/SketchEventCenter';
import { useEffect, useState } from 'react';
import { SET_RIGHT_MENU } from '../../commands/sketch/cmd-integration-dim/types';
import { userEvents } from '@app-cad/module';

function Component(props: { id: string }) {
  const [menuItems, setMenuItems] = useState(null);
  function handleItemClick(item) {
    item.action && item.action(item.params);
  }
  useEffect(() => {
    userEvents.on(SET_RIGHT_MENU, (menuItem) => {
      setMenuItems(menuItem);
    });
  }, []);
  return (
    <>
      {menuItems?.length > 0 && (
        <ContextMenu id={props.id}>
          {menuItems?.map((item) => {
            return (
              <MenuItem
                key={item.id}
                id={item.id}
                onClick={() => handleItemClick(item)}>
                {item.name}
              </MenuItem>
            );
          })}
        </ContextMenu>
      )}
    </>
  );
}

const show = (): boolean => {
  if (sketchEventCenter().state === 'sleeping') {
    return false;
  }
  if (sketchEventCenter().state === 'dimIntegration') {
    return true;
  }
  return false;
};

export const DimRightMenu: IContextMenu = {
  id: 'dimRightMenu',
  show,
  render: (id: string, params?) => (
    <Component
      id={id}
      key={`next-cad-context-menu-${id}`}
      {...params}
    />
  ),
};
